package com.jianjun.project.common.model;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jianjun.common.constant.Constants;
import com.jianjun.project.common.model.base.BaseSysUser;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Generated by JFinal.
 */
@EqualsAndHashCode(callSuper = true)
@SuppressWarnings("serial")
@Data
public class SysUser extends BaseSysUser<SysUser> {

    private SysDept dept;


    /** 角色对象 */
    private List<SysRole> roles;

    /** 岗位对象 */
    private List<SysPost> posts;

    /** 角色组 */
    private Long[] roleIds;

    /** 岗位组 */
    private Long[] postIds;

    /** 角色ID */
    private Long roleId;


    private boolean admin;
    /** 当前记录起始索引 */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Integer pageNum = 1;
    /** 每页显示记录数 */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Integer pageSize = 10;
    /** 排序列 */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String orderByColumn;
    /** 排序的方向desc或者asc */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String isAsc;

    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }

    public SysDept getDept() {
        if (Objects.nonNull(dept)) {
            return dept;
        } else if (Objects.nonNull(getDeptId())) {
            dept = new SysDept().dao().findById(getDeptId());
            return dept;
        }
        return dept;
    }

    public List<SysRole> getRoles() {
        if (Objects.nonNull(roles)) {
            return roles;
        } else if (Objects.nonNull(getUserId())) {
            SqlPara sqlParaDb = Db.getSqlPara("querySysRole", Kv.by("userId", getUserId()));
            roles = new SysRole().dao().find(sqlParaDb);
            return roles;
        }
        return roles;
    }

    public List<SysPost> getPosts() {
        if (Objects.nonNull(posts)) {
            return posts;
        } else if (Objects.nonNull(getUserId())) {
            SqlPara sqlParaDb = Db.getSqlPara("querySysPost", Kv.by("userId", getUserId()));
            posts = new SysPost().dao().find(sqlParaDb);
            return posts;
        }
        return posts;
    }

    @JsonIgnore
    public boolean getAdmin() {
        return isAdmin(this.getUserId());
    }

    @JsonIgnore
    public boolean isAdmin() {
        return isAdmin(this.getUserId());
    }

    @JsonIgnore
    public void setAdmin(boolean admin) {
        this.admin = isAdmin(this.getUserId());
    }

    @JsonIgnore
    public Date getBeginTime() {
        return getDate("beginTime");
    }

    @JsonProperty("params[beginTime]")
    public void setBeginTime(Date beginTime) {
        put("beginTime", beginTime);
    }

    @JsonIgnore
    public Date getEndTime() {
        return getDate("endTime");
    }

    @JsonProperty("params[endTime]")
    public void setEndTime(Date endTime) {
        put("endTime", endTime);
    }

    @JsonIgnore
    public Kv getColumnAsc() {
        if (StrUtil.hasEmpty(orderByColumn, isAsc)) {
            return Kv.of(Constants.COLUMN_ASC, StrUtil.EMPTY);
        }
        String tempAsc = "";
        // 兼容前端排序类型
        if ("ascending".equals(isAsc)) {
            tempAsc = "asc";
        } else if ("descending".equals(isAsc)) {
            tempAsc = "desc";
        }
        return Kv.of(Constants.COLUMN_ASC, StrUtil.toUnderlineCase(orderByColumn) + " " + tempAsc);
    }
}

